package com.sfac.javaSpringBoot.modules.test.controller;

import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.test.entity.Student;
import com.sfac.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description StudentController
 * @Author HymanHu
 * @Date 2021/9/14 15:01
 */
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student ---- post
     * {"studentName":"hujiang","age":18,"email":"hujiang@163.com"}
     * {"studentName":"hujiang1","age":19,"email":"hujiang1@163.com","card":{"cardNo":"001"}}
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"id":1,"studentName":"hujiangaa","age":20,"email":"hujiangaa@163.com"}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/2 ---- delete
     */
    @DeleteMapping("/student/{studentId}")
    public Result<Object> deleteStudentByStudentId(@PathVariable int studentId) {
        return studentService.deleteStudentByStudentId(studentId);
    }

    /**
     * 127.0.0.1/api/student/1 ---- get
     */
    @GetMapping("/student/{studentId}")
    public Student getStudentByStudentId(@PathVariable int studentId) {
        return studentService.getStudentByStudentId(studentId);
    }
}
